<template>
  <div class="consim-main">
    <div class="consim-title">
      <span class="title">{{titleText}}</span>
      <div class="right-item" >
        <span class="item">{{btnTitleText}}</span>
        <div class="item-btn" :style="{backgroundColor: colorName}" @click="changeItemType">
          <i class="el-icon-sort"></i>
        </div>
      </div>
    </div>
    <div class="consim-content">
      <consumptionItem v-for="(item, index) in itemList" :key="index" :data="item"/>
    </div>
    <div class="consim-line"></div>

    <div class="consim-title">
      <span class="title">近一周消费数据</span>
      <div class="right-select" @click="changeLineData">
        <div class="item" :class="lineDataType==='count'?'active':''" :style="{backgroundColor: lineDataType==='count'?colorName:'rgba(255,255,255,0)'}" style="left: 0">
          笔数
        </div>
        <div class="item" :class="lineDataType==='price'?'active':''" :style="{backgroundColor: lineDataType==='price'?colorName:'rgba(255,255,255,0)'}" style="right: 0">
          金额
        </div>
      </div>
    </div>
    <div class="consim-content">
      <LineChart :chart-data="newVisitis"></LineChart>
    </div>


  </div>
</template>

<script>


    import {mapGetters} from "vuex";
    import consumptionItem from "../../../../../../../../../browser/local/oracle/Saber/src/views/wel/components/consumptionItem";
    import LineChart from "../../../../../../../../../browser/local/oracle/Saber/src/views/wel/components/LineChart";

    export default {
        name: "consumption",
        components: {consumptionItem, LineChart},
        props: {
            weekData: {
                type: Array,
                default() {
                    return []
                }
            },
            itemData: {
                type: Array,
                default() {
                    return []
                }
            }
        },
        data() {
            return {
                titleText: '昨日消费数据',
                btnTitleText: '切换至一周消费数据',
                itemDataType: 'day', //day or week
                lineDataType: 'count', //count or price
                itemList: [
                    {
                        icon: 'el-icon-picture-outline',
                        title: '交易次数',
                        fieldMap: 'tradeCountMap',
                        fieldSum: 'thisTradeCountSum',
                        fieldYear: 'TradeCountYearOnYear',
                        fieldRing: 'TradeCountRingRatio',
                        type: '次',
                        showMoneyIcon: false,
                        text: '',
                        yearContrast: '',
                        yearTrend: '',//up down flat
                        monthContrast: '',
                        monthTrend: ''
                    },
                    {
                        icon: 'el-icon-picture-outline',
                        title: '实扣金额',
                        fieldMap: 'realTicketPriceMap',
                        fieldSum: 'thisRealTicketPriceSum',
                        fieldYear: 'RealTicketPriceYearOnYear',
                        fieldRing: 'RealTicketPriceRingRatio',
                        type: '元',
                        showMoneyIcon: true,
                        text: '',
                        yearContrast: '',
                        yearTrend: '',//up down flat
                        monthContrast: '',
                        monthTrend: ''
                    },
                    {
                        icon: 'el-icon-picture-outline',
                        title: '卡码用户数量',
                        fieldMap: 'cardCountMap',
                        fieldSum: 'thisCardCountSum',
                        fieldYear: 'CardCountSumYearOnYear',
                        fieldRing: 'CardCountRingRatio',
                        type: '人',
                        showMoneyIcon: false,
                        text: '',
                        yearContrast: '',
                        yearTrend: '',//up down flat
                        monthContrast: '',
                        monthTrend: ''
                    },
                    {
                        icon: 'el-icon-picture-outline',
                        title: '客单价',
                        fieldMap: 'unitPriceMap',
                        fieldSum: 'unitPrice',
                        fieldYear: 'unitPriceyearOnYear',
                        fieldRing: 'unitPriceRingRatio',
                        type: '次',
                        showMoneyIcon: false,
                        text: '',
                        yearContrast: '',
                        yearTrend: '',//up down flat
                        monthContrast: '',
                        monthTrend: ''
                    }
                ],
                newVisitis: {},
            }
        },
        computed: {
            ...mapGetters(["colorName"]),
        },
        watch: {
            weekData(val) {
                this.getLineData(val)
            },
            itemData(val) {
                this.getItemData(val)
            },
        },
        created() {

        },
        methods: {
            changeLineData() {
                if(this.lineDataType === 'count') {
                    this.lineDataType = 'price'
                } else {
                    this.lineDataType = 'count'

                }
                this.getLineData(this.weekData)
            },
            changeItemType() {
                if(this.itemDataType === 'day') {
                    this.itemDataType = 'week'
                    this.titleText = '一周消费数据'
                    this.btnTitleText = '切换至昨日消费数据'
                } else {
                    this.itemDataType = 'day'
                    this.titleText = '昨日消费数据'
                    this.btnTitleText = '切换至一周消费数据'
                }
                this.getItemData(this.itemData)
            },
            getItemData(val) {
                let mapName = ''
                let field = ''
                if (this.itemDataType === 'day') {
                    mapName = '0'
                } else {
                    mapName = '1'
                    field = '1'
                }
                for (let i = 0; i < this.itemList.length; i++) {
                    let item = this.itemList[i]
                    let fieldNameMap = item.fieldMap + mapName
                    let thisData = val[fieldNameMap]
                    this.itemList[i].text = thisData[item.fieldSum + field]
                    let yearData = parseFloat(thisData[item.fieldYear + field]).toFixed(2)
                    let monthData = parseFloat(thisData[item.fieldRing + field]).toFixed(2)
                    this.itemList[i].yearContrast = Math.abs(yearData)
                    this.itemList[i].yearTrend = yearData < 0 ? 'down' : 'up'
                    this.itemList[i].monthContrast = Math.abs(monthData)
                    this.itemList[i].monthTrend = monthData < 0 ? 'down' : 'up'
                }

            },
            getLineData(val) {
                console.log(val)
                let newData = {}
                let keys = Object.keys(val)
                keys.sort();
                for (let i = 0; i < keys.length; i++) {
                    if(this.lineDataType==='count') {
                        newData[keys[i]] = {
                            count: val[keys[i]].TRADE_COUNT,
                            yearContrast: val[keys[i]].TradeCountYearOnYear,
                            yearTrend: val[keys[i]].TradeCountYearTrend,
                            monthContrast: val[keys[i]].TradeCountRingRatio,
                            monthTrend: val[keys[i]].TradeCountMonthContrastTrend,
                            chartDataType: this.lineDataType
                        }
                    } else {
                        newData[keys[i]] = {
                            count: val[keys[i]].REAL_TICKET_PRICE,
                            yearContrast: val[keys[i]].RealTicketPriceYearOnYear,
                            yearTrend: val[keys[i]].RealTicketPriceYearTrend,
                            monthContrast: val[keys[i]].RealTicketPriceRingRatio,
                            monthTrend: val[keys[i]].RealTicketPriceMonthContrastTrend,
                            chartDataType: this.lineDataType
                        }
                    }

                }
                this.newVisitis = newData;
            }
        }
    }
</script>

<style lang="scss" scoped>
  .consim-main {
    width: 100%;
    background-color: white;
    border-radius: 10px;
    padding: 23px 0;

    .consim-title {
      padding: 0 16px;
      width: calc(100% - 32px);
      display: flex;
      justify-content: space-between;

      .title {
        font-size: 18px;
        font-weight: 400;
        color: #333333;
      }

      .right-item {
        display: flex;
        flex-direction: row;
        align-items: center;
        overflow: hidden;

        .item {
          height: 32px;
          border-radius: 10px 0 0 10px;
          padding: 0 7px;
          line-height: 32px;
          border: 1px solid #D4D4D4;
          margin-left: 7px;
          font-size: 14px;
          font-weight: 400;
          color: #666666;
        }

        .item-btn {
          border-radius: 0 10px 10px 0;
          height: 34px;
          width: 34px;
          background-color: #00a680;
          color: white;
          text-align: center;
          line-height: 34px;

          i {
            transform: rotate(90deg);
          }
        }
      }

      .right-select {
        height: 32px;
        border-radius: 10px;
        padding: 0 7px;
        border: 1px solid #D4D4D4;
        position: relative;
        width: 60px;

        .item {
          border-radius: 10px;
          top: -1px;
          position: absolute;
          width: 30px;
          height: 34px;
          text-align: center;
          line-height: 34px;
          font-size: 14px;
          font-weight: 400;
          color: #666666;
        }

        .active {
          width: 40px;
          background-color: #64a15f;
          color: white;
        }
      }
    }

    .consim-content {
      margin-top: 24px;
      padding: 0 16px;
      width: calc(100% - 32px);
      display: flex;
      flex-direction: row;
      justify-content: space-between;
    }

    .consim-line {
      width: calc(100% - 32px);
      margin: 26px auto;
      height: 1px;
      background: #F5F7FB;
    }
  }
</style>
